'use strict';

const Controller = require('egg').Controller;

class CategoryController extends Controller {
  async index() {
    const { ctx, ctx: { service, customBody }, app: { mysql } } = this;
    const platform = ctx.get('platform')
    const userInfo = ctx.userInfo
    let uid = userInfo.id;
    let { pageSize = 10, page = 1, pid } = customBody;

    let limit = pageSize,
      offset = pageSize * (page - 1);
    var where = {
      platform,
      uid, status: 0,
    }
    if (pid > -1) {
      where.pid = pid
    }


    var data = await mysql.select('admin_category', { where })
    ctx.body = {
      code: 200,
      data,
      meg: "获取成功"
    };
  }
  async store() {

    const { ctx, ctx: { service, customBody }, app: { mysql } } = this;
    const platform = ctx.get('platform')

    const userInfo = ctx.userInfo
    customBody.uid = userInfo.id;
    customBody.platform = platform;
    if (customBody.id) {
      var data = await mysql.update('admin_category', customBody);
    } else {
      var data = await mysql.insert('admin_category', customBody);
    }
    if (data.affectedRows) {
      ctx.body = {
        code: 200,
        data: null,
        meg: "添加成功"
      };
    } else {
      ctx.body = {
        code: 200,
        data: null,
        meg: "添加失败"
      };
    }
  }

  async remove() {
    const { ctx, ctx: { service, customBody }, app: { mysql } } = this;

    var where = {
      id: customBody.id,
      status: -1,
    }
    var data = await mysql.update('admin_category', where);
    ctx.body = {
      code: 200,
      data,
      meg: "删除成功"
    };
  }

  async detail() {
    const { ctx, ctx: { service, customBody }, app: { mysql } } = this;
    let { id } = customBody
    const userInfo = ctx.userInfo
    let uid = userInfo.id;
    const data = await mysql.get('admin_category', { id });
    ctx.body = {
      code: 200,
      data,
      meg: "获取成功"
    };
  }


}

module.exports = CategoryController;






